package com.zwface.demo.function.attendance;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseActivity;
import com.zwface.demo.databinding.ActivityAttendanceLsBinding;
import com.zwface.demo.function.attendance.adapter.AttendanceLsAdapter;
import com.zwface.demo.function.attendance.mvvm.AttendanceViewModel;
import com.zwface.demo.greendao.AttendanceManager;
import com.zwface.demo.greendao.UserInfoManager;
import com.zwface.demo.model.db.Attendance;
import com.zwface.demo.model.db.UserInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * created by      张中文
 * on:             2020/4/28 16:09
 * 描述：考勤列表
 */
public class AttendanceLsActivity extends BaseActivity<ActivityAttendanceLsBinding>
{
    private AttendanceViewModel viewModel;
    private LinearLayoutManager linearLayoutManager;
    private List<Attendance> mData;
    private List<Attendance> exportList;
    private AttendanceLsAdapter mAdapter;
    @Override
    public int onLayoutId() {
        return R.layout.activity_attendance_ls;
    }

    @Override
    public void onInitViewDataBinding(ActivityAttendanceLsBinding binding) {
        super.onInitViewDataBinding(binding);
        viewModel = ViewModelProviders.of(this).get(AttendanceViewModel.class);
    }

    @Override
    public void onInitComponent() {
        super.onInitComponent();
        initTopbar();
        linearLayoutManager = new LinearLayoutManager(context) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AttendanceLsAdapter();
        mAdapter.setEmptyView(R.layout.layout_empty, (ViewGroup) binding.recyclerView.getParent());
        binding.recyclerView.setAdapter(mAdapter);
        getData();
    }

    private void initTopbar(){
        binding.topbar.setTitle("考勤记录");
        binding.topbar.addLeftBackImageButton().setOnClickListener(view -> finish());
        Button rightBtn = binding.topbar.addRightTextButton("导出",R.id.topbar_right_view_button);
        rightBtn.setTextColor(Color.WHITE);
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //导出表格
                if(exportList == null||exportList.size()==0)
                {
                    showToast("无数据可导出");
                    return;
                }
                viewModel.exportExcel(context,exportList);
            }
        });
    }

    private void getData(){
        mData = AttendanceManager.queryAllAttendance();
        exportList = AttendanceManager.queryAllAttendance();
        mAdapter.setNewData(mData);

        getNotAttendanceStudents();

        binding.recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.btn_attendace:
                        //教师手动更改学生签到状态
                        UserInfo userInfo = new UserInfo();
                        userInfo.setId(mData.get(position).getStuId());
                        userInfo.setFace(mData.get(position).getFace());
                        userInfo.setStuNo(mData.get(position).getStuNo());
                        userInfo.setName(mData.get(position).getStuName());
                        viewModel.sign(userInfo);
                        getData();
                        break;
                }
            }
        });
    }

    /**
     * 获取未签到学生
     */
    private void getNotAttendanceStudents(){
        List<UserInfo> allStus = UserInfoManager.queryAllStus();
        if(mData !=null && allStus != null){
            for (Attendance a:mData){
                for (int i=0;i<allStus.size();i++){
                    UserInfo u = allStus.get(i);
                    if(a.getStuId().equals(u.getId())){
                        allStus.remove(i);
                    }
                }
            }
        }

        if(allStus !=null&&allStus.size()>0){
            List<Attendance> notAttendanceLs = new ArrayList<>();
            //把未签到的学生添加到列表中
            for (UserInfo u:allStus){
                Attendance attendance = new Attendance();
                attendance.setStuNo(u.getStuNo());
                attendance.setFace(u.getFace());
                attendance.setStuId(u.getId());
                attendance.setStuName(u.getName());
                notAttendanceLs.add(attendance);
            }

            mAdapter.addData(notAttendanceLs);
        }
    }


}
